import Router from 'koa-router'
import axios from './utils/axios'
// import Province from '../dbs/models/province'
// import City from '../dbs/models/city'

let router = new Router({prefix: '/geo'})

const sign = `ebcf2f08dd01ccfc98cf04547e4ee0eb`
const serverUrl = `http://cp-tools.cn/geo/`

router.get(`/getPosition`, async (ctx) => {
  let {status, data: {province, city}} = await axios.get(`${serverUrl}getPosition?sign=${sign}`)
  if (status === 200) {
    ctx.body = {province, city}
  } else {
    ctx.body = {
      province: '',
      city: ''
    }
  }
})
router.get(`/province`, async (ctx) => {
  // let province = await Province.find()
  // ctx.body = {
  //   province: province.map(item => {
  //     return {
  //       id: item.id,
  //       name: item.value[0]
  //     }
  //   })
  // }
  let {status, data: {province}} = await axios.get(`${serverUrl}province?sign=${sign}`)
  ctx.body = {
    province: status === 200 ? province : []
  }
})
router.get(`/province/:id`, async (ctx) => {
  let {status, data: {city}} = await axios.get(`${serverUrl}province/${ctx.params.id}?sign=${sign}`)
  ctx.body = {
    city: status === 200 ? city : []
  }
})
router.get('/city', async (ctx) => {
  // let result = await City.find({id: ctx.params.id})
  // console.log('result', result)
  // result.forEach(item => {
  // })
  // ctx.body = {
  //   city: null
  // }
  let {status, data: {city}} = await axios.get(`http://cp-tools.cn/geo/city?sign=${sign}`);
  ctx.body = {
    city: status === 200 ? city : []
  }
})
router.get('/hotCity', async (ctx) => {
  /*
  let list = ['北京市', '上海市', '广州市', '深圳市', '天津市', '西安市', '杭州市', '南京市', '武汉市', '成都市']
  let result = await City.find()
  let nList = []
  result.forEach(item => {
    nList = nList.concat(item.value.filter(k => list.includes(k.name) || list.includes(k.province)))
  })
  ctx.body = {
    hots: nList
  }
  */
  let {status, data: {hots}} = await axios.get(`http://cp-tools.cn/geo/hotCity?sign=${sign}`);
  ctx.body = {
    hots: status === 200 ? hots : []
  }
})
router.get(`/menu`, async (ctx) => {
  let {status, data: {menu}} = await axios.get(`${serverUrl}menu?sign=${sign}`)
  if (status === 200) {
    ctx.body = {menu}
  } else {
    ctx.body = {menu: []}
  }
})

export default router
